<template>
  <div class="col-md-9 box">
    <!-- 点击导出，需要插件 -->
    <img src="../assets/pic@2x/out@2x.png" alt class="out" @click='derive'>
    <el-row>
      <el-col :span="24">
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="每日产出" name="first">
            <el-col :span="24">
              <el-button round>全部时间</el-button>
              <el-button round>最近7天</el-button>
              <el-button round>最近30天</el-button>
              <div class="block">
                <el-date-picker
                  v-model="value1"
                  type="datetimerange"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                  :default-time="['08:00:00']"
                  size="mini"
                ></el-date-picker>
              </div>
            </el-col>
            <el-table
              :data="tableData"
              :cell-style="text1"
              :header-cell-style="text2"
              :default-sort="{prop: 'date', order: 'descending'}"
              style="width: 100%;"
              id="day"
            >
              <!-- //prop均未修改 -->
              <el-table-column sortable prop="date" label="时间" width="150"></el-table-column>
              <el-table-column sortable prop="name" label="操作" width="150"></el-table-column>
              <el-table-column sortable prop="address" label="币种" width="150"></el-table-column>
              <el-table-column sortable prop="name" label="数量" width="150"></el-table-column>
              <el-table-column sortable prop="address" label="账户余额"></el-table-column>
              <div slot="empty" class="empty">
                <img src="../assets/pic@2x/noData@2x.png" alt>
                <p>暂无数据...</p>
                <el-button plain>购买矿机套餐</el-button>
              </div>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="每单产出" name="second">
            <el-col :span="24">
              <el-button round>全部</el-button>
              <el-button round>生效中</el-button>
              <el-button round>已完成</el-button>
            </el-col>
            <el-table
              :data="tableData1"
              :cell-style="text1"
              :header-cell-style="text2"
              :default-sort="{prop: 'date', order: 'descending'}"
              style="width: 100%;"
              empty-text="当前数据没有，请添加数据"
              id="count"
            >
              <!-- //prop均未修改 -->
              <el-table-column sortable prop="date" label="订单号" width="150"></el-table-column>
              <el-table-column sortable prop="name" label="累计产出" width="150"></el-table-column>
              <el-table-column sortable prop="address" label="算力" width="150"></el-table-column>
              <el-table-column sortable prop="name" label="套餐剩余天数" width="150"></el-table-column>
              <el-table-column sortable prop="address" label="电费剩余天数"></el-table-column>
              <!-- 空数据样式 -->
              <div slot="empty" class="empty">
                <img src="../assets/pic@2x/noData@2x.png" alt>
                <p>暂无数据...</p>
                <el-button plain>购买矿机套餐</el-button>
              </div>
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import FileSaver from "file-saver";
export default {
  data() {
    return {
      //判断是哪个表
      tableIndex:'',
      // 导航页默认
      activeName: "first",
      //日期选择器
      value1: "",
      tableData: [
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市"
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市"
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市"
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普"
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普"
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普"
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普"
        }
      ],
      tableData1: []
    };
  },
  methods: {
    //选择框的触发事件
    handleClick(tab, event) {
      console.log(tab, event);
      //通过index确认是哪个表格
      this.tableIndex=tab.index
    },
    //单元格回调，修改样式
    text1({ row, rowIndex }) {  
      return "text-align: center;background-color:rgba(238,238,238,1);";
    },
    //表头回调，修改样式
    text2({ row, rowIndex }) {
      return "text-align: center;font-weight:400;color:rgba(0,0,0,1);background-color:rgba(238,238,238,1);";
    },
    derive(){
      var wb;
      if(this.tableIndex==0){
         wb = XLSX.utils.table_to_book(document.querySelector("#day"));
      }else{
         wb = XLSX.utils.table_to_book(document.querySelector("#count"));
      }
      var wbout = XLSX.write(wb, {
            bookType: "xlsx",
            bookSST: true,
            type: "array"
        });
        try {
            FileSaver.saveAs(
            new Blob([wbout], { type: "application/octet-stream" }),
            //设置导出文件名称
            "table.xlsx"
            );
        } catch (e) {
            if (typeof console !== "undefined") console.log(e, wbout);
        }
        return wbout;
        }
  }
};
</script>
<style lang="less" scoped>
.box {
  background-color: #fff;
  padding: 0 60px 200px 60px;
  .out {
    position: absolute;
    z-index: 9999;
    right: 60px;
    top: 45px;
  }
  //空白区域
  /deep/.el-table__empty-text {
    width: 100%;
    line-height: 40px;
    .empty {
      padding: 50px 0;
      width: 100%;
      background-color: rgba(238, 238, 238, 1);
      p {
        margin: 0;
      }
      .el-button {
        padding: 9px 20px;
        color: #171c61;
        border: 1px solid #171c61;
        cursor: pointer;
      }
    }
  }

  //导航条
  /deep/.el-tabs__header {
    background-color: #fff;
    border-radius: 5px;
    overflow: hidden;
    .el-tabs__active-bar {
      margin-left: 30px;
    }
    /deep/.el-table td,
    .el-table th.is-leaf {
      padding: 18px 0;
    }
    .el-tabs__item {
      height: 81px;
      line-height: 110px;
      margin-left: 30px;
      font-size: 21px;
      &:nth-child(3) {
        margin-left: 0;
      }
    }
  }
  //button
  .el-button.is-round {
    padding: 5px 16px;
    margin: 30px 20px 40px 0px;
    &:nth-child(1) {
      margin-left: 30px;
    }
  }
  //日期选择器
  .block {
    width: 50%;
    display: inline-block;
    text-align: right;
    .el-date-editor--datetimerange.el-input__inner {
      width: 81%;
    }
    /deep/.el-date-editor .el-range-input {
      //   width: 60px;
      color: rgba(100, 100, 100, 1);
    }
  }
}
</style>